import { ref } from "vue"
import type { Ref } from "vue";
import { useRouter } from "vue-router";
import { getSearchList } from "@/api/search"
import { getProductList } from "@/api/search"
type defaultType = {
    algSort: boolean;
    extra: {
        alg_personalized: string;
        itemFrom: number;
        materialContentFrom: number;
        rcmdSort: boolean;
        resourcesId: number;
        taskType: number;
    };
    hidden: number;
    highlight: number;
    keyword: string;
    schemeUrl: string;
    type: number;
};
type productType = {
    name: string;
    id: number;
    primaryPicUrl: string;
    retailPrice: number;
    listPicUrl: string;
    sortOriginPrice: number
    finalPriceInfoVO: finalPriceInfoVOType
}
export type finalPriceInfoVOType = {
    banner: {
        content: string;
        logo: string;
        price: number;
        title: string | null;
    };
}
export function useSearch() {
    let value: Ref<string> = ref("");
    let router = useRouter()
    let flag: Ref<boolean> = ref(true)
    let defaultKeyword: Ref<Array<defaultType>> = ref([])
    let hotKeywordVOList: Ref<Array<defaultType>> = ref([])
    let productList: Ref<Array<productType>> = ref([])
    getSearchList()
        .then(res => {
            console.log(res);
            defaultKeyword.value = res.data.data.defaultKeyword
            hotKeywordVOList.value = res.data.data.hotKeywordVOList
            console.log(defaultKeyword);
            console.log(hotKeywordVOList);
        })
    const onClick = function (url: string, name: string) {
        if (url !== null) {
            location.href = url
        }
        value.value = name;
        flag.value = !flag.value;
        getProductList(name).then(res => {
            console.log(res);
            productList.value = res.data.data.directlyList
            console.log(productList);
        })

    }
    const toDetail = function (id:number) {
        router.push("/detail/" + id)
    }
    const onSearch = function () {

    }

    const onCancel = function () {
        if (flag.value === true) {
            router.back()
        } else {
            flag.value = !flag.value;
        }
    }

    return {
        value, onSearch, onCancel, onClick, defaultKeyword, hotKeywordVOList, flag, productList, toDetail
    }
}